package com.xtsoft.platform.base.system.controller;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import com.xtsoft.core.controller.GenericController;

import com.xtsoft.core.repository.QueryFilter;
import com.xtsoft.core.repository.QueryResult;
import com.xtsoft.core.utils.BeanUtil;
import com.xtsoft.core.utils.ResultMsg;
import com.xtsoft.core.utils.StringUtils;
import com.xtsoft.core.utils.tree.CommonTreeNode;
import com.xtsoft.platform.base.system.domain.SysOrganize;
import com.xtsoft.platform.base.system.domain.SysOrganizeType;
import com.xtsoft.platform.base.system.service.SysOrganizeService;
import com.xtsoft.platform.base.system.service.SysOrganizeTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.List;


 /**
 * 
 * @ClassName:  SysOrganizetype   
 * <p> 企业类型处理类 </p>
 * @author: Codegen-代码生成器
 * @date:   2019年12月3日 上午10:59:46
 */ 
@Tag(name = "企业类型--处理接口")
@RestController
public class SysOrganizeTypeController extends GenericController{
	
	private final Logger log = LoggerFactory.getLogger(SysOrganizeTypeController.class);
	
	@Autowired	
	private SysOrganizeTypeService sysOrganizetypeService;
	
	@Autowired
	private SysOrganizeService sysOrganizeService;
	
	
    /**
     *  <p>新增一个企业类型</p>  
     * @param sysOrganizetype 要新增的企业类型
     * @return ResponseEntity<ResultMsg>, ResultMsg的Data中包含 企业类型
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常                     
	 * @author Codegen-代码生成器
	 * @date 2022-02-23 16:31:45
    */
	@Parameter(name = "sysOrganizetype", description = "要新增的企业类型", required = false)
	@Operation(summary ="新增，根据前台传递的企业类型新增")
    @PostMapping("/sysOrganizetype/add")

    public ResponseEntity<ResultMsg> add(@RequestBody SysOrganizeType sysOrganizetype) throws URISyntaxException {
		if(null == sysOrganizetype){
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"新增失败，提交的记录为空"),HttpStatus.OK); 
		}
		log.info("新增企业类型开始......");
        if (!StringUtils.isEmpty(sysOrganizetype.getOrganizeTypeId())) {
		    log.info("新增企业类型失败，新企业类型不能具有OrganizeTypeId属性值");
            return new ResponseEntity<ResultMsg>(new ResultMsg(false,"新增失败，新记录不能具有唯一标识值"),HttpStatus.OK);
        }
        sysOrganizetypeService.addSysOrganizeType(sysOrganizetype);
        SysOrganizeType result = sysOrganizetypeService.save(sysOrganizetype);
		log.info("新增企业类型成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"新增成功",result),HttpStatus.OK);
    }	

    /**
     * <p>更新一个企业类型</p>    
     * @param sysOrganizetype 要更新的企业类型
     * @return ResponseEntity<ResultMsg>, json格式
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常
	 * @author Codegen-代码生成器
	 * @date 2022-02-23 16:31:45
     */
	@Parameter(name = "sysOrganizetype", description = "要更新的企业类型", required = false)
	@Operation(summary ="更新，根据传递的 企业类型更新实体")
    @PostMapping("/sysOrganizetype/update")

	
    public ResponseEntity<ResultMsg> update(@RequestBody SysOrganizeType sysOrganizetype) throws URISyntaxException, IllegalAccessException, InvocationTargetException {
		if(null == sysOrganizetype){
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"更新失败，提交的记录为空"),HttpStatus.OK); 
		}
		log.info("单条更新企业类型开始......");
		//若 企业类型无主键值，说明调用错误
        if (StringUtils.isBlank(sysOrganizetype.getOrganizeTypeId())) {
			log.info("单条更新企业类型失败，企业类型对象OrganizeTypeId无属性值");
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"更新失败，更新的记录无唯一标识值"),HttpStatus.OK);
        }
        //若类存在存在关联关系，前台传递过来的关联属性大概率为空，执行以下处理
		SysOrganizeType result = sysOrganizetypeService.get(sysOrganizetype.getOrganizeTypeId());
		BeanUtil.copyNotNullProperties(result, sysOrganizetype);
		result = sysOrganizetypeService.save(result);
		log.info("单条更新企业类型成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"更新成功",result),HttpStatus.OK);
    }  

    /**
	 *   <p>查询企业类型列表，若分页，则页码信息在Request的参数中，包括当前页，每页记录数</p>
     * @return ResponseEntity<ResultMsg>，json格式
     * @throws URISyntaxException 如果出错，则产生页码信息的 HTTP headers
	 * @author Codegen-代码生成器
	 * @date 2022-02-23 16:31:45
     */
	
	@Operation(summary ="查询，根据传递的条件查询企业类型列表，条件参数在Request中")
    @GetMapping("/sysOrganizetype/getAll")

	
    public ResponseEntity<QueryResult<SysOrganizeType>> getAll()
        throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
		log.info("获取企业类型列表开始......");
        QueryFilter<SysOrganizeType> filter=new QueryFilter<SysOrganizeType>(SysOrganizeType.class);
        filter.addFilterFromRequest(request);
        QueryResult<SysOrganizeType> result= sysOrganizetypeService.anyQuery(filter);
		log.info("获取企业类型列表成功");	
        return new ResponseEntity< QueryResult<SysOrganizeType>>(result,HttpStatus.OK);
    }

    /**
	 *   <p>根据企业类型唯一ID值获取企业类型</p>
     * @param id 企业类型唯一ID值
     * @return  ResponseEntity<SysOrganizetype>
	 * @author Codegen-代码生成器
	 * @date 2022-02-23 16:31:45		
     */
	@Parameter(name = "id", description="对象唯一Id", required = false)
	@Operation(summary ="获取单个企业类型，若根据企业类型唯一Id获取到了多个对象则发生异常")
    @GetMapping("/sysOrganizetype/get/{id}")
	
    public ResponseEntity<ResultMsg> get(@PathVariable String id) {
		if(StringUtils.isBlank(id)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取单条记录失败，未给定记录的唯一标识"),HttpStatus.OK);
		}
		log.info("获取企业类型开始......");
		SysOrganizeType sysOrganizetype = sysOrganizetypeService.get(id);
		/*
		*  <p> ofNullable执行逻辑：如果对象为空，则执行orElse，否则执行.map...
		*		return Optional.ofNullable(sysRole).map(result -> new ResponseEntity<>(result, HttpStatus.OK))
		*		.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));		
		*/
		
		if( null == sysOrganizetype ){
			log.info("获取企业类型失败");	
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取单条记录失败，提交的记录可能被删除"),HttpStatus.OK);
		}
		log.info("获取企业类型成功");
		return new ResponseEntity<ResultMsg>(new ResultMsg(true,"获取成功",sysOrganizetype),HttpStatus.OK);
    } 

    /**
     *     <p>根据企业类型唯一ID值删除单个企业类型</p>
     * @param  id 企业类型唯一ID值
     * @return ResponseEntity,json格式
	 * @author Codegen-代码生成器
	 * @date 2022-02-23 16:31:45		 
     */
	@Parameter(name = "id", description="企业类型唯一Id", required = false)
	@Operation(summary ="删除单个企业类型") 
    @GetMapping("/sysOrganizetype/delete/{id}")
	
    public ResponseEntity<ResultMsg> delete(@PathVariable String id) {
		if(StringUtils.isBlank(id)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"删除单条记录失败，未给定记录的唯一标识"),HttpStatus.OK);
		}
		log.info("删除单条企业类型开始......");
		List<SysOrganize> sysOrganizes = sysOrganizeService.findBySysOrganizeTypeId(id);
		if(sysOrganizes.size()>0) {
			 return new ResponseEntity<ResultMsg>(new ResultMsg(true,"删除失败！该企业类型已被企业所引用！"),HttpStatus.OK);
		}
		sysOrganizetypeService.remove(id);
		log.info("删除单条企业类型成功");		
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"删除成功"),HttpStatus.OK);
    }
	
    /**
     *     <p>根据企业类型唯一ID值集合，批量删除企业类型</p>
     * @param  ids 企业类型唯一ID值集合
     * @return ResponseEntity<ResultMsg>,返回结果中有删除的记录数，json格式
	 * @author Codegen-代码生成器
	 * @date 2022-02-23 16:31:45		 
     */
	@Parameter(name = "ids", description="逗号分隔的企业类型Id集合", required = false)
	@Operation(summary ="删除企业类型集合") 
    @PostMapping("/sysOrganizetype/deleteBatch")
	
    public ResponseEntity<ResultMsg> deleteBatch(@RequestParam String ids) {
		if(StringUtils.isBlank(ids)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"批量删除记录失败，未给定记录的唯一标识集合"),HttpStatus.OK);
		}		
		log.info("批量删除企业类型开始......");
        long rows = sysOrganizetypeService.deleteBatch(ids);
		log.info("批量删除企业类型成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"批量删除记录成功",rows),HttpStatus.OK);
    }
	
    /**
     *     <p>根据企业类型唯一ID值集合，批量更新企业类型</p>
     * @param  ids 企业类型唯一ID值集合
	 * @param  properityName 企业类型要更新的属性名，不带类名
	 * @param  properityValue 企业类型要更新的属性值
     * @return ResponseEntity<ResultMessage>,返回结果中有更新的记录数，json格式
	 * @author Codegen-代码生成器
	 * @date 2022-02-23 16:31:45		 
     */
	
	@Parameters({
	@Parameter(name = "properityName", description="要更新的属性名不带类名，只能是类单个属性名且不能是集合类型", required = false),
	@Parameter(name = "properityValue", description="属性值", required = false),
	@Parameter(name = "ids", description="逗号分隔的企业类型Id集合", required = false)
	})
	@Operation(summary ="更新企业类型集合") 
    @PostMapping("/sysOrganizetype/updateBatch")
	
    public ResponseEntity<ResultMsg> updateBatch(@RequestParam String properityName,
		@RequestParam String properityValue,
		@RequestParam String ids) {
		if(StringUtils.isBlank(ids) || StringUtils.isBlank(properityValue) || StringUtils.isBlank(properityName)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"批量更新记录失败，未给定记录的唯一标识集合，或未给定属性名、或属性值"),HttpStatus.OK);
		}		
		log.info("批量更新企业类型开始......");
        long rows = sysOrganizetypeService.updateBatch(properityName,properityValue,ids);
		log.info("批量更新企业类型成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"批量更新记录成功",rows),HttpStatus.OK);
    }	
    

	@Operation(summary  = "获取企业树")
	@GetMapping("/sysOrganizeType/getTree")
	public ResponseEntity<List<CommonTreeNode>> getTree() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException{
		return new ResponseEntity<List<CommonTreeNode>>(sysOrganizetypeService.getTree(), HttpStatus.OK);
	}
}
